﻿Imports sonicERPinfo
Imports System.Collections.Generic
Imports System.Web
Imports System.Web.Security
Imports System.Web.HttpResponse





Public Class SessionManagerModule
    Implements IHttpModule

    Private expirou As Boolean = False


    Private _app As HttpApplication



    Public Sub Dispose() Implements System.Web.IHttpModule.Dispose

    End Sub
    Private Sub _app_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
        If (Me._app.Request.Path <> FormsAuthentication.LoginUrl) Then
            Dim authCookie As HttpCookie = Me._app.Request.Cookies(FormsAuthentication.FormsCookieName)
            If Not authCookie Is Nothing AndAlso Not String.IsNullOrEmpty(authCookie.Value) Then
                Dim ticket As FormsAuthenticationTicket = FormsAuthentication.Decrypt(authCookie.Value)
                If ticket.Expired Then
                    expirou = True

                    Dim cookie1 As HttpCookie = New HttpCookie(FormsAuthentication.FormsCookieName, "")
                    Me._app.Response.Cookies.Add(cookie1)
                    Dim cookie2 As HttpCookie = New HttpCookie("ASP.NET_SessionId", "")
                    cookie2.Expires = DateTime.Now.AddYears(-1)
                    Me._app.Response.Cookies.Add(cookie2)
                    Me._app.Response.Cache.SetCacheability(HttpCacheability.NoCache)
                    Me._app.CompleteRequest()
                    FormsAuthentication.RedirectToLoginPage("Authentication=Expired")
                End If
            End If

        End If
       




    End Sub

    Public Sub Init(ByVal context As System.Web.HttpApplication) Implements System.Web.IHttpModule.Init
        Me._app = context
        AddHandler Me._app.BeginRequest, AddressOf Me._app_BeginRequest


    End Sub




End Class


